package com.lihang;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import me.yugy.github.shadowlibrary.ResourceTable;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.Component;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import org.jetbrains.annotations.Nullable;

/**
 * Created by leo
 * on 2020/8/3.
 */
class GlideRoundUtils {
    public static void setRoundCorner(final Component view, final Element resourceId, final float cornerDipValue) {

        if (cornerDipValue == 0) {
            if (view.getEstimatedWidth() == 0 && view.getEstimatedHeight() == 0) {
                view.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
                    @Override
                    public void onRefreshed(Component component) {
                        view.setLayoutRefreshedListener(null);
                        Glide.with(view.getContext())
                                .asDrawable()
                                .load(((PixelMapElement) resourceId).getPixelMap())
                                .transform(new CenterCrop())
                                .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                                .into(new CustomTarget<Element>() {
                                    @Override
                                    public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                        view.setBackground(resource);
                                    }

                                    @Override
                                    public void onLoadCleared(@Nullable Element placeholder) {

                                    }
                                });
                    }
                });
            } else {
                Glide.with(view.getContext())
                        .asDrawable()
                        .load(((PixelMapElement) resourceId).getPixelMap())
                        .transform(new CenterCrop())
                        .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                        .into(new CustomTarget<Element>() {
                            @Override
                            public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                view.setBackground(resource);
                            }

                            @Override
                            public void onLoadCleared(@Nullable Element placeholder) {

                            }
                        });
            }

        } else {
            if (view.getEstimatedWidth() == 0 && view.getEstimatedHeight() == 0) {
                view.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
                    @Override
                    public void onRefreshed(Component component) {
                        view.setLayoutRefreshedListener(null);
                        Glide.with(view.getContext())
                                .load(((PixelMapElement) resourceId).getPixelMap())
                                .transform(new CenterCrop(), new RoundedCorners((int) cornerDipValue))
                                .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                                .into(new CustomTarget<Element>() {
                                    @Override
                                    public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                        view.setBackground(resource);
                                    }

                                    @Override
                                    public void onLoadCleared(@Nullable Element placeholder) {

                                    }
                                });
                    }
                });
            } else {
                Glide.with(view.getContext())
                        .load(((PixelMapElement) resourceId).getPixelMap())
                        .transform(new CenterCrop(), new RoundedCorners((int) cornerDipValue))
                        .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                        .into(new CustomTarget<Element>() {
                            @Override
                            public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                view.setBackground(resource);
                            }

                            @Override
                            public void onLoadCleared(@Nullable Element placeholder) {

                            }
                        });
            }

        }

    }


    public static void setCorners(final Component view, final Element resourceId, final float leftTop_corner, final float leftBottom_corner, final float rightTop_corner, final float rightBottom_corner) {
        if (leftTop_corner == 0 && leftBottom_corner == 0 && rightTop_corner == 0 && rightBottom_corner == 0) {
            if (view.getEstimatedWidth() == 0 && view.getEstimatedHeight() == 0) {
                view.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
                    @Override
                    public void onRefreshed(Component component) {
                        view.setLayoutRefreshedListener(null);
                        Glide.with(view.getContext())
                                .load(((PixelMapElement) resourceId).getPixelMap())
                                .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                                .into(new CustomTarget<Element>() {
                                    @Override
                                    public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                        view.setBackground(resource);
                                    }

                                    @Override
                                    public void onLoadCleared(@Nullable Element placeholder) {

                                    }
                                });
                    }
                });
            } else {
                Glide.with(view.getContext())
                        .load(((PixelMapElement) resourceId).getPixelMap())
                        .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                        .into(new CustomTarget<Element>() {
                            @Override
                            public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                view.setBackground(resource);
                            }

                            @Override
                            public void onLoadCleared(@Nullable Element placeholder) {

                            }
                        });
            }

        } else {
            if (view.getEstimatedWidth() == 0 && view.getEstimatedHeight() == 0) {
                view.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() {
                    @Override
                    public void onRefreshed(Component component) {
                        view.setLayoutRefreshedListener(null);
                        GlideRoundTransform transform = new GlideRoundTransform(view.getContext(), leftTop_corner, leftBottom_corner, rightTop_corner, rightBottom_corner);
                        Glide.with(view.getContext())
                                .load(((PixelMapElement) resourceId).getPixelMap())
                                .transform(transform)
                                .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                                .into(new CustomTarget<Element>() {
                                    @Override
                                    public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                        view.setBackground(resource);
                                    }

                                    @Override
                                    public void onLoadCleared(@Nullable Element placeholder) {
                                    }

                                    @Override
                                    public void onLoadFailed(Element element) {
                                        super.onLoadFailed(element);
                                    }
                                });
                    }
                });
            } else {

                GlideRoundTransform transform = new GlideRoundTransform(view.getContext(), leftTop_corner, leftBottom_corner, rightTop_corner, rightBottom_corner);
                Glide.with(view.getContext())
                        .load(((PixelMapElement) resourceId).getPixelMap())
                        .transform(transform)
                        .override(view.getEstimatedWidth(), view.getEstimatedHeight())
                        .into(new CustomTarget<Element>() {
                            @Override
                            public void onResourceReady(Element resource, @Nullable Transition<? super Element> transition) {
                                view.setBackground(resource);
                            }

                            @Override
                            public void onLoadCleared(@Nullable Element placeholder) {

                            }
                        });

            }

        }

    }

}
